package com.meishij.customerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.meishij.R;

/**
 * Created by Administrator on 2016/10/15.
 * 我是youngKaka 我的心愿是世界和平
 */

public class ShiHua_Quick_index_View extends View {

    private Paint paint;
    private float cellHeight;
    private int widthSize;
    private int lastPos = -1;
    private int currentId = -1;

    private String[] indexArr = {"A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};
    private GetLetterListener listener;

    public ShiHua_Quick_index_View(Context context) {
        this(context, null);
    }

    public ShiHua_Quick_index_View(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShiHua_Quick_index_View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(60);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < indexArr.length; i++) {
            float x=widthSize/2;
            float y=getTextHeight(indexArr[i])/2+i*cellHeight+cellHeight/2;
            if(currentId==i){
                paint.setColor(getResources().getColor(R.color.main_radio_cheack));
                canvas.drawText(indexArr[i],x,y,paint);
            }else{
                paint.setColor(Color.BLACK);
                canvas.drawText(indexArr[i],x,y,paint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                currentId= ((int) (event.getY() / cellHeight));
                break;
            case MotionEvent.ACTION_UP:
                lastPos=-1;
                currentId=-1;
                break;
        }
        if (currentId != lastPos) {
            lastPos = currentId;
        }
        if (listener != null && currentId >= 0 && currentId < 26) {
            listener.getLetter(indexArr[currentId]);

        }
        //重绘
        invalidate();
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        widthSize = MeasureSpec.getSize(widthMeasureSpec);
        cellHeight = heightSize * 1.0f / indexArr.length;
    }


    public int getTextHeight(String text) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, 1, rect);
        return rect.height();
    }

    public interface GetLetterListener {
        void getLetter(String text);
    }

    public void setLetterListener(GetLetterListener listener) {
        this.listener = listener;
    }

}
